package ContainerLayer;
import java.util.ArrayList;

/**
 * Write a description of class ProductContainer here.
 * 
 * Will as container for ProductDescription
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ProductContainer
{
    // instance variables - replace the example below with your own   
    private ArrayList<ProductDescription> descriptions; 
    private static ProductContainer instance;    
    private long storeNo;

    /**
     * Constructor for objects of class ProductContainer
     */
    public ProductContainer()
    {
        // initialise instance variables
        descriptions = new ArrayList<ProductDescription>();      

    }

    public static ProductContainer getInstance()
    {
        if(instance == null)
        {
            instance = new ProductContainer();
        }
        return instance;       
    }

    public void addProductDescription(ProductDescription product)
    {
        if(!descriptions.equals(product))
        {
            descriptions.add(product);
        }
        else
        {
            System.out.println("There is already a product with the typed description");
        }
    }

    public ProductDescription readProductDescription(long id)
    {
        int index = 0;
        int descriptionIndex = 0;
        boolean found = false;

        while(index < descriptions.size() && !found)
        {
            if(descriptions.get(index).getID() == id)
            {
                found = true;
                descriptionIndex = index;
            }
            index++;
        }
        if(found)
        { 
            return descriptions.get(descriptionIndex);
        }
        else
        {
            return null;
        }

    }

    public void updateProductDescription(long id, String description, double price, int minNo)
    {
        int index = 0;
        int descriptionIndex = 0;
        boolean found = false;       

        while(index < descriptions.size() && !found)
        {
            if(descriptions.get(index).getID() == id)
            {
                found = true;
                descriptionIndex = index;
            }
            index++;
        }
        if(found)
        {
            ProductDescription d = descriptions.get(descriptionIndex);            
            if(price != 0)
            {
                d.setPrice(price);
            }            
            if(description != null)
            {
                d.setDescription(description);
            }

            if(minNo != 0)
            {
                d.setMinNo(minNo);
            }           

        }
        else
        {
            System.out.println("There is no product description with the typed ID,  - try again with another ID");
        }

    }

    public void deleteProductDescription(long id)
    {
        int index = 0;
        int descriptionIndex = 0;
        boolean deleted = false;       

        while(index < descriptions.size() && !deleted)
        {
            if(descriptions.get(index).getID() == id)
            {
                deleted = true;
                descriptionIndex = index;
            }
            index++;
        }
        if(deleted)
        {
            descriptions.remove(descriptionIndex);
        }
        else
        {
            System.out.println("There is no product description with the typed ID, - try again with another ID");
        }
    }

    public void printAllProductDescription()
    {
        System.out.println("    SHOWS INFORMATION ABOUT ALL REGISTERED Product descriptions");
        if(descriptions != null)
        {
            for(ProductDescription d : descriptions)
            {
                System.out.println("############################################################");
                System.out.println("#  Product ID: "+d.getID());
                System.out.println("#  ---------------------------------------------------------");
                System.out.println("# description: "+d.getDescription()); 
                System.out.println("#       price: "+d.getPrice());
                System.out.println("#      min no: "+d.getMinNo());                  
                System.out.println("#");
                System.out.println("########   THE END  for Product ID: "+d.getID());
                System.out.println("");                
            }
        }
        else
        {
            System.out.println("           >>There is no product registred<<");            
        }
    }

    public void printOneProductDescription(long id)
    {    

        if(readProductDescription(id) != null)
        {                        
            ProductDescription d = readProductDescription(id);

            System.out.println("############################################################");
            System.out.println("#  Product ID: "+d.getID());
            System.out.println("#  ---------------------------------------------------------");
            System.out.println("# description: "+d.getDescription()); 
            System.out.println("#       price: "+d.getPrice());
            System.out.println("#      min no: "+d.getMinNo()); 
            if(getProductNoSize(id) > 0)
            {System.out.println("#    store no: "+getProductNoSize(id));}            
            else
            {System.out.println("#    store no: 0  !! no products on store");}
            System.out.println("#");
            System.out.println("########   THE END  for Product ID: "+d.getID());
            System.out.println(""); 
            if(getProductNoSize(id) < getProductDescriptionMinimum(id))
            {System.out.println("      THERE IS NOT ENOUGH PRODUCT ON STORE");}
        }
        else
        {

        }

    } 

    public int getProductDescriptionSize()
    { return descriptions.size();}

    public long getProductDescriptionMinimum(long id)
    { 
        if(readProductDescription(id) != null)
        {
            ProductDescription d = readProductDescription(id);
            return d.getMinNo();
        }
        else
        {
            return 0;
        }
    }

    public int getProductNoSize(long id)
    {
        ProductDescription pd = readProductDescription(id);
        return pd.getProductNoSize();
    }

    public long getStoreNo()
    {
        return storeNo;
    }

    public void createProductNoToProductDescription(long id, boolean storeStatus)
    {
        storeNo++;
        ProductDescription pd = readProductDescription(id);
        ProductNo productNo = new ProductNo(storeNo, storeStatus);        

        pd.addProductNo(productNo);

    }

    public ProductNo readProductNoToProductionDescription(long id, long storeNo)
    {
        ProductDescription pd = readProductDescription(id);

        return pd.readProductNo(storeNo);
    }

    public void updateProductNoToProductDescription(long id, long storeNo, boolean storeStatus)
    {
        ProductDescription pd = readProductDescription(id);

        pd.updateProductNo(storeNo, storeStatus);

    }

    public void deleteProductNoToProductDescription(long id, long storeNo)
    {
        ProductDescription pd = readProductDescription(id);

        pd.deleteProductNo(storeNo);

    }

    public void printOneProductNoToProductDescription(long id, long storeNo)
    {
        ProductDescription pd = readProductDescription(id);

        pd.printOneProductNo(storeNo);
    }

    public void printAllProductNoToProductDescription(long id)
    {
        ProductDescription pd = readProductDescription(id);

        pd.printAllProductNo();
    }

}
